#!/bin/sh /etc/rc.common
# Copyright (C) 2015-2016 wongsyrone

. /lib/functions.sh

START=95

EXTRA_COMMANDS="status flush libver"
EXTRA_HELP=\
"	flush	Flush DNS cache stored in Pcap_DNSProxy and DNSMasq
	libver	Print library version Pcap_DNSProxy linked to
	status	Show the running status of Pcap_DNSProxy"

SERVICE_DAEMONIZE=1

PROG=/usr/sbin/Pcap_DNSProxy
CONFDIR=/etc/pcap-dnsproxy
HOTPLUGFILE=/tmp/pcap-dnsproxy.hotplug
ERRLOG=$CONFDIR/Error.log

config_load "pcap-dnsproxy"

parse_pcap_dnsproxy()
{
	config_get ENABLED "$section" "enabled"
}

config_foreach parse_pcap_dnsproxy 'pcap-dnsproxy'

boot() {
	# wait a given time (default 10 seconds) before startup
	# to wait for interfaces to come up / not using hotplug events during boot
	_start() {
		[ $1 -gt 0 ] && {
			logger -p daemon.info -t "pcap-dnsproxy[]" "Scheduled startup in $1 seconds"
			sleep $1
		}
		start
	}

	local _DELAY
	_DELAY=$(uci_get "pcap-dnsproxy" "system" "boot_delay" "10")
	_start $_DELAY &
	return 0
}

shutdown() {
	rm -f $HOTPLUGFILE
	stop
}

start() {
	[ "1" = "$ENABLED" -o "on" = "$ENABLED" -o "true" = "$ENABLED" ] && {
		[ $(ps|grep ${PROG}|grep -v grep|wc -l) -ge 1 ] && {
			echo "Pcap_DNSProxy is already running, no need to start again"
			exit 1
		}
		[ -f "$ERRLOG" ] && {
			echo "Removing previous ${ERRLOG##*/}"
			rm -f "$ERRLOG" >/dev/null 2>&1
		}
		# do NOT use daemon code inside pcap-dnsproxy, use start-stop-daemon instead
		service_start $PROG -c $CONFDIR --disable-daemon
		# create hotplug mark, it will update the last-modified date if the file exists
		touch $HOTPLUGFILE
		local RESULT=$?
		# wait to check error log file
		sleep 1s
		[ -f "$ERRLOG" ] \
			&& echo "WARNING: ${ERRLOG##*/} exists, check its content and other config files in ${CONFDIR}" \
			|| echo "Hooray! ${ERRLOG##*/} not found"
		[ $RESULT -eq 0 -a $(ps|grep ${PROG}|grep -v grep|wc -l) -ge 1 ] && {
			local _PID=$(pidof ${PROG##*/})
			echo "Pcap_DNSProxy started successfully"
			logger -p daemon.notice -t "Pcap_DNSProxy[$_PID]" "Started successfully"
		} || {
			echo "Pcap_DNSProxy failed to start"
			logger -p daemon.warn -t "Pcap_DNSProxy[]" "Failed to start"
			exit 1
		}
	} || {
		echo "Pcap_DNSProxy is disabled, please check /etc/config/pcap-dnsproxy for more info"
	}
}

stop() {
	# If you can NOT terminate Pcap_DNSProxy, try 'killall' instead.
	# e.g. killall -SIGTERM Pcap_DNSProxy
	[ $(ps|grep ${PROG}|grep -v grep|wc -l) -ge 1 ] && {
		local _PID=$(pidof ${PROG##*/})
		service_stop $PROG
		local RESULT=$?
		sleep 1s
		[ $RESULT -eq 0 -a $(ps|grep ${PROG}|grep -v grep|wc -l) -eq 0 ] && {
			echo "Pcap_DNSProxy terminated successfully"
			logger -p daemon.notice -t "Pcap_DNSProxy[$_PID]" "Shutdown successfully"
			rm -rf "/tmp/pcap_dnsproxy_fifo"
		} || {
			echo "Failed to terminate Pcap_DNSProxy"
			logger -p daemon.warn -t "Pcap_DNSProxy[$_PID]" "Failed to terminate"
			echo "Retry or use 'killall -SIG{TERM,KILL} Pcap_DNSProxy' instead"
		}
	} || {
		echo "Pcap_DNSProxy is not running, no need to terminate"
	}
}

restart() {
	stop
	sleep 5s
	start
}

flush() {
	[ $(ps|grep ${PROG}|grep -v grep|wc -l) -ge 1 ] && {
		local _PID=$(pidof ${PROG##*/})
		$PROG --flush-dns
		logger -p daemon.notice -t "Pcap_DNSProxy[$_PID]" "Flush message sent"
	} || {
		echo "Pcap_DNSProxy is not running, I can NOT flush DNS cache for you."
		exit 1
	}
}

libver() {
	$PROG --lib-version
}

status() {
	[ $(ps|grep ${PROG}|grep -v grep|wc -l) -ge 1 ] \
		&& echo "Pcap_DNSProxy is running, PID is $(pidof ${PROG##*/})" \
		|| echo "Pcap_DNSProxy is NOT running"
}
